﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mighty.Common;


namespace Mighty.Mighty
{
    public partial class FriendForm : Form
    {
        GameInfo gameinfo = new GameInfo();
        public FriendPlayer friendPlayer = new FriendPlayer();
        Player[] players = new Player[5];

        public FriendForm(GameInfo g)
        {
            gameinfo = g;
            InitializeComponent();
        }

        private void ChoiceFriend()
        {
            if (rdoMighty.Checked)
            {
                friendPlayer.howToSelect = FriendPlayer.HowToSelect.card;
                friendPlayer.cardCondition = gameinfo.mighty;
            }
            else if (rdoJoker.Checked)
            {
                Card joker = new Card();
                joker.shape = Shape.Joker;
                friendPlayer.howToSelect = FriendPlayer.HowToSelect.card;
                friendPlayer.cardCondition = joker;
            }
            else if (rdoNoFriend.Checked)
            {
                friendPlayer.howToSelect = FriendPlayer.HowToSelect.nofriend;

            }
            else if (rdoCard.Checked)
            {
                Card c = new Card();

                friendPlayer.howToSelect = FriendPlayer.HowToSelect.card;
                c.shape = GetShapeFromTxt(cmbShape.Text);
                c.number = GetNumFromTxt(cmbNumber.Text);
                friendPlayer.cardCondition = c;

            }
            else if (rdoWinner.Checked)
            {
                friendPlayer.howToSelect = FriendPlayer.HowToSelect.firstwin;
            }
            else if (rdoPlayer.Checked)
            {
                friendPlayer.howToSelect = FriendPlayer.HowToSelect.player;
                friendPlayer.playerCondition = players[cmbPlayer.SelectedIndex];

            }
        }


        private void rdoSelf_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoCard.Checked)
            {
                cmbNumber.Visible = true;
                cmbShape.Visible = true;
                labNumber.Visible = true;
                labShape.Visible = true; ;
            }
            else
            {
                cmbNumber.Visible = false;
                cmbShape.Visible = false;
                labNumber.Visible = false;
                labShape.Visible = false;
            }
        }


        private void rdoPlayer_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoPlayer.Checked)
            {
                cmbPlayer.Visible = true;
                labPlayer.Visible = true;
            }
            else
            {
                cmbPlayer.Visible = false;
                labPlayer.Visible = false;
            }
        }


        public int GetNumFromTxt(string txt)
        {
            if (txt == "A")
            {
                return 14;
            }
            else if (txt == "K")
            {
                return 13;
            }
            else if (txt == "Q")
            {
                return 12;
            }
            else if (txt == "J")
            {
                return 11;
            }
            else
            {
                return Convert.ToInt16(txt);
            }

        }

        public Shape GetShapeFromTxt(string txt)
        {
            Shape result = new Shape();
            if (txt == "♣클로버")
            {
                result = Shape.Clover;
            }
            else if (txt == "◆다이아몬드")
            {
                result = Shape.Diamond;
            }
            else if (txt == "♥하트")
            {
                result = Shape.Heart;
            }
            else if (txt == "♠스페이드")
            {
                result = Shape.Spade;
            }
            return result;

        }

        private void FriendForm_Load(object sender, EventArgs e)
        {
            // 위치 설정
            cmbPlayer.Location = new Point(63, 153);
            labPlayer.Location = new Point(19, 156);

            cmbNumber.Location = new Point(63, 168);
            labNumber.Location = new Point(19, 171);

            cmbShape.Location = new Point(63, 138);
            labShape.Location = new Point(19, 141);

            cmbNumber.Text = "A";
            cmbShape.Text = "♥하트";

            int myPosition = gameinfo.myInfo.position;
            int i = 0;

            foreach (Player p in gameinfo.players)
            {
                if (p.position != myPosition)
                {
                    players[i] = new Player();
                    players[i] = p;

                    string nick = p.ToString();
                    if (p.nick != "") nick = string.Format("#{0} {1}", p.position, p.nick);

                    cmbPlayer.Items.Add(nick);
                    i++;
                }
            }
            cmbPlayer.Text = gameinfo.players[(myPosition + 1) % gameinfo.players.Length].nick;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            ChoiceFriend();
            this.Close();
        }
    }
}
